package com.fs.uc.core.relationship;

import com.fs.uc.base.domain.Org;
import com.fs.uc.base.domain.OrgType;
import com.fs.uc.base.repo.OrgRepository;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

/**
 * @author 崔超
 * 2022/1/7
 */
@ApplicationScoped
class OrgRelationFetcher implements IRelationFetcher {
    @Inject
    OrgRepository orgRepo;

    @Override
    public Long inWhichEnterprise(Long orgId) {
        Org org = orgRepo.getById(orgId);
        if (org.type == OrgType.Park) {
            throw new RuntimeException("园区主体不应该属于某个企业主体");
        }
        return org.id;
    }

    @Override
    public Long inWhichPark(Long orgId) {
        Org org = orgRepo.getById(orgId);
        if (org.type == OrgType.Park) {
            return org.id;
        }else{
            if (org.parent == null) {
                throw new RuntimeException("企业主体没有上级园区");
            }
            return org.parent.id;
        }
    }
}
